package com.tn.service.gate.impl;

import cn.hutool.core.util.StrUtil;
import com.tn.controller.alarm.common.model.CommonVO;
import com.tn.controller.alarm.common.model.PageResult;
import com.tn.controller.alarm.common.model.gate.vo.GateLogInfoVO;
import com.tn.controller.alarm.common.model.gate.vo.GateLogQueryVO;
import com.tn.dao.gate.IGateDao;
import com.tn.dao.mbpdao.Gateloginfo;
import com.tn.dao.mbpdao.GateloginfoMapper;
import com.tn.service.gate.IGateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 闸口服务实现类
 *
 * @program: tn_aucy_java
 * @author: yangjiayao
 * @create: 2021/11/16
 */
@Slf4j
@Service
public class GateServiceImpl implements IGateService {
    /**
     * 闸口数据库交互Dao
     */
    IGateDao gateDao;

    @Resource
    GateloginfoMapper gateloginfoMapper;

    /**
     * 自动注入
     *
     * @param gateDao gateDao
     */
    @Autowired
    public void setGateDao(IGateDao gateDao) {
        this.gateDao = gateDao;
    }

    @Override
    public CommonVO<PageResult<GateLogInfoVO>> currentGateInfo(Integer curPage, Integer pageSize) {
        CommonVO<PageResult<GateLogInfoVO>> resultCommonVO = new CommonVO<>();
        int totalCount = gateDao.queryCurrentGateInfoCount();
        PageResult<GateLogInfoVO> result = new PageResult<>(curPage, pageSize, totalCount);
        result.setData(gateDao.queryCurrentGateLogInfoByPage(result.getStartIndex(), pageSize));
        resultCommonVO.setData(result);
        return resultCommonVO;
    }

    @Override
    public CommonVO<PageResult<GateLogInfoVO>> getGateLogInfo(Integer curPage, Integer pageSize,
                                                              GateLogQueryVO queryVO) {
        CommonVO<PageResult<GateLogInfoVO>> resultCommonVO = new CommonVO<>();
        int totalCount = gateDao.queryGateLogInfoCount(queryVO);
        PageResult<GateLogInfoVO> result = new PageResult<>(curPage, pageSize, totalCount);
        result.setData(gateDao.queryGateLogInfoByPage(result.getStartIndex(), pageSize, queryVO));
        resultCommonVO.setData(result);
        return resultCommonVO;
    }

    @Override
    public CommonVO<Object> updateCurrentGateInfo(String gateId, String license) {
        if(StrUtil.isNotBlank(gateId) && StrUtil.isNotBlank(license)) {
            Gateloginfo info = new Gateloginfo();
            info.setGateid(gateId);
            info.setLicense(license);
            gateloginfoMapper.updateById(info);
        }
        return new CommonVO<>();
    }
}
